<template>
  <div class="coupon_index">
    <div class="top">
      <div class="fixed">
        <span :class="[tagType == 0 ? 'selected' : '']" @click="changeTag(0)"
          >可使用{{
            canUseLength !== undefined ? "(" + canUseLength + ")" : ''
          }}</span
        >
        <span :class="[tagType == 1 ? 'selected' : '']" @click="changeTag(1)"
          >已使用{{
            usedLength !== undefined ? "(" + usedLength + ")" : ''
          }}</span
        >
        <span :class="[tagType == 2 ? 'selected' : '']" @click="changeTag(2)"
          >已过期{{
            expiredLength !== undefined ? "(" + expiredLength + ")" : ''
          }}</span
        >
        <span :class="[tagType == 3 ? 'selected' : '']" @click="changeTag(3)">
          核销记录{{
            hexiaoLength !== undefined ? "(" + hexiaoLength + ")" : ''
          }}
        </span>
      </div>
    </div>
    <div class="center">
      <div
        class="coupon"
        :class="getClassNames(ele.couponTemplateInfo.couponType)"
        v-for="ele in couponList"
        :key="ele.couponUserId"
        @click="gotoHexiao(ele)"
      >
        <div class="left" v-if="tagType != 3">
          <div
            class="amount"
            v-if="['1', '4', '5'].includes(ele.couponTemplateInfo.couponType)"
          >
            ￥<span class="value">{{ ele.couponTemplateInfo.couponAmt }}</span>
          </div>
          <div class="amount" v-if="ele.couponTemplateInfo.couponType == 2">
            <span class="value">{{ ele.couponTemplateInfo.couponAmt }}</span
            >%
          </div>
          <div class="condition" v-if="ele.couponTemplateInfo.couponType == 5">
            每升立减
          </div>
          <div class="condition">
            {{
              ele.couponIssueInfo.couponThresholdAmt == 0
                ? "无门槛"
                : "满" + ele.couponIssueInfo.couponThresholdAmt + "元可用"
            }}
          </div>
          <div class="exchangeName">
            {{ ele.couponTemplateInfo.couponName }}
          </div>
        </div>
        <div class="left" v-else>
          <span>{{ getCouponText(ele.couponTemplateInfo.couponType) }}</span>
          <div class="exchangeName">
            {{ ele.couponTemplateInfo.couponName }}
          </div>
        </div>
        <div class="right" v-if="tagType != 3">
          <div class="title">
            <span class="type">
              {{ getCouponText(ele.couponTemplateInfo.couponType) }}
            </span>
            <span class="state">
              {{ tagType === 2 ? "已失效" : "" }}
              {{ tagType === 1 ? "已使用" : "" }}
              {{ tagType === 0 && ele.canUse == "1" ? "可使用" : "" }}
              {{ tagType === 0 && !ele.canUse == "1" ? "暂时不可用" : "" }}
            </span>
          </div>
          <div
            class="content"
            :class="[ele.couponTemplateInfo.couponType != 3 ? 'notip' : '']"
            style="position: relative;"
          >
            <div class="items">
              <div
                class="item valid"
                v-if="ele.couponTemplateInfo.couponType != 3"
              >
                {{ ele.isOilType === 1 ? '商品' : '油品' }}：<span>{{ ele.oils }}</span>
              </div>
              <div class="item scope">
                适用：
                <span class="hidden-scroll">
                  {{
                    ["1", "2", "4", "5"].includes(
                      ele.couponTemplateInfo.couponType
                    )
                      ? ele.couponStaNames
                      : ""
                  }}{{
                    ele.couponTemplateInfo.couponType == 3
                      ? ele.verStaNames
                      : ""
                  }}
                </span>
              </div>
              <div class="item valid">
                截止：<span>{{ ele.couponEffectiveTime }}</span>
              </div>
              <div class="item from">
                来源：<span>{{ ele.couponIssueInfo.issueName }}</span>
              </div>
            </div>
          </div>

          <!-- 二维码 -->
          <div
            class="btn"
            style="position: absolute;right: 0.2rem;top: 0rem;width:0.5rem"
            v-if="
              tagType === 0 &&
                ['4', '1', '2'].includes(ele.couponTemplateInfo.couponType)
            "
          >
            <img style="max-width:100%" src="@/static/coupon/showqrcode.png" />
            <!-- <button
                @click="goVerification(ele.couponNo, ele.oils)"
                v-if="tagType === 0"
              >
                出示
              </button> -->
          </div>
          <div class="tip" v-if="ele.couponTemplateInfo.couponType == 3">
            {{ ele.couponTemplateInfo.couponDetails }}
          </div>
        </div>
        <div class="right2" v-else>
          <p class="p1">
            优惠券类型:
            {{ getCouponText(ele.couponTemplateInfo.couponType) }}
          </p>
          <div class="infoItme">
            <p>
              优惠券额度:
              {{
                ele.couponTemplateInfo.couponType == 2
                  ? `${ele.couponTemplateInfo.couponAmt}%`
                  : ele.couponTemplateInfo.couponAmt || "-"
              }}
            </p>
          </div>
          <div class="infoItme">
            <p>核销人：{{ ele.verificationUserAccount || "-" }}</p>
          </div>
          <div class="infoItme">
            <p>
              核销方式:{{
                ele.verificationType === 1
                  ? "自助核销"
                  : ele.verificationType === 2
                  ? " pos机核销"
                  : "-"
              }}
            </p>
            <p>核销张数：{{ ele.couponCount || "-" }}</p>
          </div>
          <p class="time">核销时间：{{ ele.verificationTime || "-" }}</p>
        </div>
      </div>
      <!--
      <div class="coupon expired">
        <div class="left">
          <div class="amount">￥<span class="value">100</span></div>
          <div class="condition">满1000元可用</div>
        </div>
        <div class="right">
          <div class="title">
            <span class="type">立减劵</span><span class="state">已过期</span>
          </div>
          <div class="content">
            <div class="items">
              <div class="item scope">适用：<span>汽油</span></div>
              <div class="item valid">截止：<span>2021.6.3</span></div>
              <div class="item from">来源：<span>加油活动</span></div>
            </div>
            <div class="btn">
              <button>已领</button>
            </div>
          </div>

          <div class="tip">凭借此劵可以减少支付的金额，欢迎来加油~AA哈行啊</div>
        </div>
      </div>
      -->
    </div>
    <div
      v-if="couponList.length == 0"
      style="
        text-align: center;
        font-weight: bold;
        margin-top: 2rem;
        margin-bottom: 3rem;
      "
    >
      暂未符合条件的优惠劵
    </div>
    <van-pagination
      v-model="queryForm.pageNum"
      :total-items="total"
      :items-per-page="queryForm.pageSize"
      mode="simple"
      @change="getCouponList"
      v-if="total > queryForm.pageSize"
    >
      <template #prev-text>
        <van-icon name="arrow-left" />
      </template>
      <template #next-text>
        <van-icon name="arrow" />
      </template>
      <template #page="{ text }">{{ text }}</template>
    </van-pagination>
    <div class="bottom">
      <div>更多活动，请关注我们的公众号</div>
      <div>上海深斯能源科技有限公司提供技术支持</div>
    </div>
    <!-- {{$store.state.parse.issueId}} -->
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
import Vue from "vue";
import { Pagination, Icon } from "vant";

Vue.use(Icon);
Vue.use(Pagination);

export default {
  head() {
    return {
      title: "我的优惠券"
    };
  },
  data() {
    return {
      couponList: [],
      currentPage: 1,
      total: 0,
      queryForm: {
        pageNum: 1, //第几页
        pageSize: 8, // 容量
        // enterpriseId: 123,
        // enterpriseId: this.enterpriseId,
        enterpriseId: 7,
        // unionId: this.unionId,
        // unionId: "o65PX6a8gYoXrRtsay4nid7vaN-k",
        couponIsUsed: 0,
        mobilePhone: ""
      },
      canUseLength: undefined,
      usedLength: undefined,
      expiredLength: undefined,
      hexiaoLength: undefined,
      tagType: 0
    };
  },
  computed: {
    ...mapGetters({
      userInfo: "authen/userInfo"
    })
    // chargeList() {
    //   return this.allList.filter((ele) => {
    //     return ele.usageType === "+";
    //   });
    // },
    // payList() {
    //   return this.allList.filter((ele) => {
    //     return ele.usageType === "-";
    //   });
    // },
  },
  filters: {},
  components: {},
  created() {
    // const unionId = this.unionId;
    // if (!unionId) {
    //   return;
    // }
    this.tagType = Number(this.$route.query.couponType) || 0;
    this.changeTag(this.tagType);
  },
  methods: {
    ...mapMutations({
      setCouponId: "verification/setCouponId",
      setVerificationName: "verification/setVerificationName",
      setCouponInfo: "verification/setCouponInfo"
    }),
    // 匹配优惠券样式
    getClassNames(couponType) {
      return {
        direct: couponType == 1,
        rate: couponType == 2,
        exchange: couponType == 3,
        carwash: couponType == 4,
        reduction: couponType == 5,
        tongyong: couponType == 100,
        expired: this.tagType == 2
      };
    },
    // 匹配优惠券类型名称
    getCouponText(couponType) {
      const couponTypeMap = [
        { type: "1", text: "现金券" },
        { type: "2", text: "折扣券" },
        { type: "3", text: "兑换劵" },
        { type: "4", text: "洗车券" },
        { type: "5", text: "立减券" },
        { type: "100", text: "优惠券" }
      ];
      let couponText = "";
      const matchedCoupon = couponTypeMap.find(ele => ele.type == couponType);
      if (matchedCoupon) {
        couponText = matchedCoupon.text;
      }
      return couponText;
    },
    changeTag(type) {
      switch (type) {
        case 0:
          this.queryForm.pageNum = 1;
          this.queryForm.couponIsUsed = 0;
          this.tagType = 0;
          this.getCouponList();
          break;
        case 1:
          this.queryForm.pageNum = 1;
          this.queryForm.couponIsUsed = 1;
          this.tagType = 1;
          this.getCouponList();
          break;
        case 2:
          this.queryForm.pageNum = 1;
          this.queryForm.couponIsUsed = 2;
          this.tagType = 2;
          this.getCouponList();
          break;
        case 3:
          this.queryForm.pageNum = 1;
          this.queryForm.couponIsUsed = 3;
          this.tagType = 3;
          this.getCouponList();
          break;
        default:
          break;
      }
    },

    // 获取优惠券列表
    getCouponList() {
      this.queryForm.enterpriseId = this.enterpriseId;
      this.queryForm.mobilePhone = this.phone;
      // this.queryForm.unionId = this.unionId;

      return this.$axios
        .$get("/selectUserCoupons", {
          params: this.queryForm
        })
        .then(res => {
          console.log("haha ", res);
          if (res.retCode === 0) {
            // if(!!res.data.list){

            // }
            this.couponList = res.data.list;
            // this.couponList
            this.total = res.data.total;

            switch (this.tagType) {
              case 0:
                this.canUseLength = res.data.total;
                break;
              case 1:
                this.usedLength = res.data.total;
                break;
              case 2:
                this.expiredLength = res.data.total;
                break;
              case 3:
                this.hexiaoLength = res.data.total;
                break;
              default:
                break;
            }
          } else {
            throw new Error(res.message);
          }
        })
        .catch(res => {
          alert("拉取优惠券失败了");
        });
    },
    // goVerification(id, verificationName) {
    //   this.setCouponId(id);
    //   this.setVerificationName(verificationName);
    //   this.$router.push({
    //     path: "/" + this.enterpriseId + "/coupon/verification/"
    //   });
    // },
    verificationNameToString(arr) {
      // let str = "";
      // arr.map((ele) => {
      //   str += ele.verificationName + " ";
      // });
      // return str;
    },
    // 去核销
    gotoHexiao(couponInfo) {
      console.log("优惠券：", couponInfo);
      // couponType : 1 立减券  2 折扣券 3 兑换券  4 洗车券,
      //  1现金劵，2折扣券，3兑换券,4洗车券；
      //  tagType ： 券的使用状态： 0 可使用   1 已使用
      if (this.tagType == 3) {
        this.$router.push({
          path: "/" + this.enterpriseId + "/coupon/autoHexiao_detail",
          query: {
            dataInfo: JSON.stringify(couponInfo),
            back: 1,
            couponType: this.tagType
          }
        });
      }
      if (
        couponInfo.couponTemplateInfo.couponType == 3 ||
        this.tagType === 1 ||
        this.tagType === 3
      ) {
        console.log("此优惠券暂不支持扫码河核销或已核销");
        return;
      }
      this.setCouponInfo(couponInfo);
      this.setCouponId(couponInfo.couponNo);
      this.setVerificationName(couponInfo.oils);
      this.$router.push({
        path: "/" + this.enterpriseId + "/coupon/verification/"
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.coupon_index {
  width: 7.5rem;
  background: #ffffff;
  padding: 0 0.3rem 0 0.3rem;
  box-sizing: border-box;
  min-height: 100vh;

  > .top {
    width: 6.9rem;
    height: 1rem;

    .fixed {
      position: fixed;
      top: 0;
      left: 0;
      width: 7.5rem;
      height: 1rem;
      padding: 0 0.3rem 0 0.3rem;
      box-sizing: border-box;
      background: #ffffff;
      display: flex;
      justify-content: space-around;
      align-items: center;
      z-index: 99;
      span {
        height: 0.45rem;
        font-size: 0.32rem;
        font-weight: 400;
        color: #aaaaaa;
        line-height: 0.45rem;
        letter-spacing: 0.01rem;
      }
      span.selected {
        color: #111111;
      }
    }
  }
  > .center {
    .coupon {
      display: flex;
      justify-content: space-between;
      height: 2.8rem;
      margin-bottom: 0.3rem;
      box-shadow: 0rem 0rem 0.1rem 0.04rem rgba(233, 223, 223, 0.5);
      > .left {
        display: flex;
        flex-direction: column;
        justify-content: center;
        text-align: center;
        width: 2.2rem;
        height: 2.8rem;
        text-align: center;
        span {
          color: #fff;
          font-size: 0.35rem;
        }
        .amount {
          display: block;
          font-size: 0.4rem;
          font-weight: 500;
          color: #ffffff;
          line-height: 0.56rem;
          text-align: center;
          margin-top: 0rem;
          .value {
            font-size: 0.6rem;
          }
        }
        .condition {
          font-size: 0.24rem;
          font-weight: 400;
          color: #ffffff;
          line-height: 0.33rem;
          text-align: center;
          margin-top: 0.25rem;
        }
        .exchangeName {
          display: none;
          font-size: 0.35rem;
          font-weight: 500;
          color: #ffffff;
          line-height: 0.56rem;
        }
      }
      > .right {
        width: 4.56rem;
        height: 2.45rem;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin: 0.08rem 0 0.05rem 0;
        position: relative;
        .title {
          height: 0.3rem;
          font-size: 0.24rem;
          font-weight: 600;
          line-height: 0.33rem;
          .type {
            width: 1.13rem;
            height: 0.3rem;
            line-height: 0.3rem;
            display: inline-block;
            background: #f8c;
            border-radius: 0.19rem;
            font-size: 0.24rem;
            font-weight: 600;
            color: #ffffff;
            text-align: center;
          }
          .state {
            margin-left: 0.2rem;
            color: #7f7f7f;

            height: 0.33rem;
            font-size: 0.24rem;
            font-weight: 600;
            line-height: 0.33rem;
          }
        }
        .content {
          display: flex;
          justify-content: space-between;
          margin: 0.05rem 0 0.05rem 0;
          height: 1.4rem;

          .items {
            width: 4.3rem;
            height: 100%;
            display: flex;
            justify-content: space-around;
            flex-direction: column;
            .item {
              color: #272626;
              font-size: 0.21rem;
              > span {
                width: 3.3rem;
                display: inline-block;
                color: #272626;
                font-size: 0.22rem;
                font-weight: bold;
                overflow: scroll; /* 超出部分隐藏 */
                white-space: nowrap; /* 文本不换行 */
              }
            }
            .from {
              display: block;
            }
          }
          > .btn {
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin-right: 0.1rem;
            > button {
              height: 0.6rem;
              background: linear-gradient(143deg, #fad965 0%, #f3b235 100%);
              border-radius: 0.2rem;
              border: none;
              outline: none;
              color: #ffffff;
              line-height: 0.33rem;
              font-size: 0.3rem;
              font-weight: 400;
              overflow: hidden; /* 超出部分隐藏 */
              white-space: nowrap; /* 文本不换行 */
              display: none;
            }
          }
        }
        .notip {
          height: 1.8rem;
        }
        .tip {
          margin-right: 0.2rem;
          border-top: 0.02rem dashed #d8d8d8;
          height: 0.49rem;
          font-size: 0.22rem;
          font-weight: 400;
          color: #7f7f7f;
          line-height: 0.23rem;
          padding-top: 0.12rem;
          box-sizing: border-box;
          overflow: scroll; /* 超出部分隐藏 */
          white-space: nowrap;
          /*  text-overflow: ellipsis;*/
        }
      }
      > .right2 {
        width: 4.56rem;
        height: 2.45rem;
        margin: 0.08rem 0 0.05rem 0;
        position: relative;
        box-sizing: content-box;
        padding: 0.1rem;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .p1 {
          font-size: 0.28rem;
        }
        > p {
          padding: 0;
          margin: 0;
          margin-bottom: 0.2rem;
        }
        .infoItme {
          display: flex;
          align-items: center;
          justify-content: space-between;
          color: #555;
          font-size: 0.25rem;
          margin-bottom: 0.1rem;
          p {
            margin: 0;
            padding: 0;
          }
        }
        .time {
          font-size: 0.28rem;
          color: #999;
          margin-bottom: 0px;
        }

        .content {
          display: flex;
          justify-content: space-between;
          margin: 0.05rem 0 0.05rem 0;
          height: 1.4rem;

          .items {
            width: 4.3rem;
            height: 100%;
            display: flex;
            justify-content: space-around;
            flex-direction: column;
            .item {
              color: #272626;
              font-size: 0.21rem;
              > span {
                width: 3.3rem;
                display: inline-block;
                color: #272626;
                font-size: 0.22rem;
                font-weight: bold;
                overflow: scroll; /* 超出部分隐藏 */
                white-space: nowrap; /* 文本不换行 */
              }
            }
            .from {
              display: block;
            }
          }
          > .btn {
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin-right: 0.1rem;
            > button {
              height: 0.6rem;
              background: linear-gradient(143deg, #fad965 0%, #f3b235 100%);
              border-radius: 0.2rem;
              border: none;
              outline: none;
              color: #ffffff;
              line-height: 0.33rem;
              font-size: 0.3rem;
              font-weight: 400;
              overflow: hidden; /* 超出部分隐藏 */
              white-space: nowrap; /* 文本不换行 */
              display: none;
            }
          }
        }
        .notip {
          height: 1.8rem;
        }
        .tip {
          margin-right: 0.2rem;
          border-top: 0.02rem dashed #d8d8d8;
          height: 0.49rem;
          font-size: 0.22rem;
          font-weight: 400;
          color: #7f7f7f;
          line-height: 0.23rem;
          padding-top: 0.12rem;
          box-sizing: border-box;
          overflow: scroll; /* 超出部分隐藏 */
          white-space: nowrap;
          /*  text-overflow: ellipsis;*/
        }
      }
    }

    .tongyong {
      > .left {
        background: url("../../../static/coupon/quan6.png") no-repeat;
        background-size: 100% 100%;
      }

      > .right {
        > .title > .type {
          background: #19c79b;
        }

        > .content > .btn button {
          background: linear-gradient(143deg, #21d9b4 0%, #0fb582 100%);
          display: block;
        }
      }
    }
    .carwash {
      > .left {
        background: url("../../../static/coupon/quan5.2x.png") no-repeat;
        background-size: 100% 100%;
      }

      > .right {
        > .title > .type {
          background: #19c79b;
        }

        > .content > .btn button {
          background: linear-gradient(143deg, #21d9b4 0%, #0fb582 100%);
          display: block;
        }
      }
    }

    .direct {
      > .left {
        background: url("../../../static/coupon/quan1.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #f8ce57;
        }
        > .content > .btn button {
          background: linear-gradient(143deg, #fad965 0%, #f3b235 100%);
        }
      }
    }
    .reduction {
      > .left {
        background: url("../../../static/coupon/lijian.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #eb6ba4;
        }
        > .content > .btn button {
          background: linear-gradient(143deg, #f57eb2 0%, #ec4e95 100%);
        }
      }
    }
    .rate {
      > .left {
        background: url("../../../static/coupon/quan2.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #eb8573;
        }
        > .content > .btn button {
          background: linear-gradient(143deg, #f4a796 0%, #e67462 100%);
        }
      }
    }
    .exchange {
      > .left {
        background: url("../../../static/coupon/quan3.2x.png") no-repeat;
        background-size: 100% 100%;
        > .amount,
        > .condition {
          display: none;
        }
        > .exchangeName {
          display: inline-block;
          width: 1.5rem;
          margin: 0 auto;
        }
      }
      > .right {
        > .title > .type {
          background: #19c79b;
        }
        > .content {
          > .items {
            width: 3.3rem;
            .item span {
              width: 2.3rem;
            }
          }
          > .btn button {
            background: linear-gradient(143deg, #21d9b4 0%, #0fb582 100%);
            display: block;
          }
        }
      }
    }
    .expired {
      > .left {
        background: url("../../../static/coupon/quan4.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #bfbfbf;
        }
        > .content > .btn button {
          display: none;
        }
      }
    }
  }
  > .bottom {
    margin-top: 0.8rem;
    margin-bottom: 0.3rem;
    text-align: center;
    font-size: 0.22rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.25);
    line-height: 0.32rem;
    letter-spacing: 1px;
  }
}
.hidden-scroll::-webkit-scrollbar {
  display: none;
}
</style>
